package cn.yuanqiao.web.controller.es;

import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.es.domain.ElasticsearchQuery;
import cn.yuanqiao.es.domain.QueryFile;
import cn.yuanqiao.es.service.ElasticsearchApiService;
import cn.yuanqiao.es.service.FileListService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 全文检索
 *
 * @ProjectName: ruoyi-modules-system
 * @Package: cn.yuanqiao.es.controller
 * @ClassName: ApiElasticSearchController
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/6/9 16:36
 * @Version: 1.0
 */
@RestController
@RequestMapping("/es/es/api")
public class ApiElasticSearchController extends BaseController {

    @Autowired
    ElasticsearchApiService elasticsearchApiService;

    @Autowired
    FileListService fileListService;


    @PostMapping("/tess")
    public AjaxResult tess(@RequestBody() List<Map<String, Object>> list) {
        elasticsearchApiService.tess(list);
        return success();
    }

    /**
     * 查询
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('es:api:search')")
    @PostMapping("/search")
    public TableDataInfo search(@RequestBody() QueryFile query) {
        return elasticsearchApiService.search(query);
    }

    /**
     * 联想查询
     *
     * @return
     */
  //  @PreAuthorize("@ss.hasPermi('es:api:searchAssociate')")
    @GetMapping("/searchAssociate")
    public AjaxResult searchAssociate(@RequestParam(value = "key") String key) {
        return AjaxResult.success(elasticsearchApiService.searchAssociate(key));
    }

    /**
     * 档案编研-档案资料查询
     *
     * @param state 1 个人 2 专题 3 公共"
     * @param ztId  选择专题的话  专题id
     * @param query 查询条件
     * @return
     */
    //@RequiresPermissions("es:api:search:communal")
    @PostMapping("/communal/search")
    public TableDataInfo searchCommunal(@RequestParam(value = "state") String state,
                                        @RequestParam(required = false, value = "ztId") String ztId,
                                        @RequestBody() QueryFile query) {
        return elasticsearchApiService.searchCommunal(query, state, ztId);
    }

    /**
     * 创建和重建索引
     *
     * @return
     * @param,
     */

    @PreAuthorize("@ss.hasPermi('es:api:insertDoc')")
    @PostMapping("/insertDoc")
    public AjaxResult insertDoc(@RequestBody(required = false) List<Map<String, Object>> map) {
        return elasticsearchApiService.createIndex(map);
    }

    /**
     * 全量索引
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('es:api:completeIndex')")
    @PostMapping("/completeIndex")
    public void completeIndex(@RequestBody(required = false) List<String> fileTableName) {
        elasticsearchApiService.completeIndex(fileTableName);
    }

    /**
     * 增量索引
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('es:api:cincrementIndex')")
    @PostMapping("/cincrementIndex")
    public void cincrementIndex(@RequestBody() List<String> fileTableName) {
        elasticsearchApiService.cincrementIndex(fileTableName);
    }

    /**
     * 增量索引 定时任务
     *
     * @return
     */
    @PostMapping("/job/cincrementIndex")
    public AjaxResult cincrementIndexJob(@RequestBody() String tenantCode) {
        return elasticsearchApiService.cincrementIndexJob(tenantCode);
    }

    /**
     * 全量索引 定时任务
     *
     * @return
     */
    @PostMapping("/job/completeIndex")
    public AjaxResult completeIndexJob(@RequestBody() String tenantCode) {
        return elasticsearchApiService.completeIndexJob(tenantCode);
    }

    /**
     * 根据id删除索引
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('es:api:dropIndex')")
    @PostMapping("/dropIndex")
    public AjaxResult dropIndex(@RequestBody(required = false) List<String> ids) {
        return elasticsearchApiService.dropIndex(ids);
    }

    /**
     * 查询全文检索时电子件匹配的字段
     *
     * @param
     * @return java.lang.String
     * @author hbWang
     * @date 2023/11/21 10:23
     */
    @PostMapping("/search/fullText")
    public String queryFullText(@RequestBody() ElasticsearchQuery query) {
        return elasticsearchApiService.queryMultiMatch(query);
    }


    /**
     * 索引维护列表接口
     *
     * @param
     * @return cn.yuanqiao.common.core.domain.AjaxResult
     * @author hbWang
     * @date 2023/6/29 18:07
     */
    @PostMapping("/queryFileList")
    public TableDataInfo queryFileList(@RequestBody(required = false) QueryFile queryFile) {
        List<Map<String, Object>> query = fileListService.queryFileList(queryFile);
        return getDataTable(query);
    }

    /**
     * 查询当前用户门类信息
     *
     * @param
     * @return cn.yuanqiao.common.core.domain.AjaxResult
     * @author hbWang
     * @date 9:51
     */
    @PostMapping("/categoryList")
    public AjaxResult categoryList(@RequestParam(value = "menuId") String menuId, @RequestParam(value = "qzh", required = false) String qzh) {
        return AjaxResult.success(fileListService.categoryList(menuId, qzh));
    }

    /**
     * 查询展示列
     *
     * @param json
     * @return
     */
    @PostMapping("/field/list")
    public AjaxResult queryFiels(@RequestBody() JSONObject json) {
        return AjaxResult.success(fileListService.queryFields(json));
    }

    /**
     * 查询条件
     *
     * @param json
     * @return
     */
    @PostMapping("/apply/list")
    public AjaxResult queryApply(@RequestBody() JSONObject json) {
        return AjaxResult.success(fileListService.queryApply(json));
    }

}
